<?php
// 引入工具类
require_once './util/DButil.php';
// 获取请求action参数
$action = $_GET['action']?$_GET['action']:'';
// 返回前端的数据封装
$result = array();

if ($action == 'login'){  // 登录
    // 获取登录参数
    $jsonObj = json_decode(file_get_contents("php://input"),true);
    $u_phone = trim($jsonObj['u_phone']?$jsonObj['u_phone']:'');
    $u_pwd = trim($jsonObj['u_pwd']?$jsonObj['u_pwd']:'');
    // 查询数据库
    $sql = "select * from userinfo where u_phone=? and u_pwd=?";
    $user =  selectList($sql,array($u_phone,$u_pwd));

    // 判断是否存在
    if($user!=null and count($user)>0){  // 登录成功
        // 开启session
        session_start();
        // 存用户信息
        $_SESSION['userinfo'] = $user[0];
        // 封装数据
        $result['state'] = 200;
        $result['message'] = '登录成功';
        $result['userinfo'] = $user[0];
    }else{
        // 封装数据
        $result['state'] = 100;
        $result['message'] = '用户名密码有误';
    }
    echo json_encode($result);

}elseif ($action == 'getSession'){
    // 开启session
    session_start();
    echo json_encode($_SESSION['userinfo']);

}elseif ($action == 'resister'){
    // 获取数据
    $obj = json_decode(file_get_contents("php://input"),true);
    $sql = "insert into userinfo values(null,?,?,?,?,?,'1',default ,now())";
    echo insAndDelAndUpd($sql,array($obj['u_phone'],$obj['u_pwd'],$obj['u_name'],$obj['address'],$obj['gender']));

}elseif ($action == 'getSession'){

}elseif ($action == 'getSession'){

}else{
    echo '别调皮';
}
